package com.ts.proj.json;

import com.alibaba.fastjson.JSONArray;
import com.ts.base.controller.BaseController;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.proj.entity.SystemButtonEntity;
import com.ts.proj.entity.SystemMenuEntity;
import com.ts.proj.service.SystemButtonService;
import com.ts.proj.service.SystemMenuService;
import com.ts.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class SystemMenuJson extends BaseController {

    @Autowired
    private SystemMenuService systemMenuService;

    @Autowired
    private SystemButtonService systemButtonService;

    /**
     * 后台分页
     * @return
     */
    @RequestMapping("/menu-list-json")
    public Map<String,Object> roleListJson(){
        PageData pageData = this.getPageData();
        Integer page = Integer.valueOf(pageData.get("page").toString().equals("0")?"1":pageData.get("page").toString());
        Integer rows = Integer.valueOf(pageData.get("rows").toString());
        PageData pd = new PageData();
        pd.put("parentId", "0");
        pd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        Long count = systemMenuService.count(pd);
        List<SystemMenuEntity> parentMenus = systemMenuService.findAll(pd,page-1, rows);
        List<PageData> pdList = new ArrayList<>();

        PageData first = new PageData();
        first.put("id", 0);
        first.put("menuName", "系统页面树形结构图");
        first.put("parentId", "-3");
        pdList.add(first);
        for(SystemMenuEntity pMenu:parentMenus){
            PageData menu = new PageData();
            menu.put("id", pMenu.getId());
            menu.put("menuName", pMenu.getMenuName());
            menu.put("menuURL", pMenu.getMenuURL());
            menu.put("menuIcon", pMenu.getMenuIcon());
            menu.put("_parentId", pMenu.getParentId());
            menu.put("parentId", pMenu.getParentId());
            menu.put("state", "closed");
            pdList.add(menu);
            pd.put("parentId", pMenu.getId());
            pd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
            List<SystemMenuEntity> sonMenus = systemMenuService.findAll(pd);
            for(SystemMenuEntity sMenu: sonMenus){
                PageData son = new PageData();
                son.put("id", sMenu.getId()+1000);
                son.put("menuName", sMenu.getMenuName());
                son.put("menuURL", sMenu.getMenuURL());
                son.put("_parentId", sMenu.getParentId());
                son.put("parentId", sMenu.getParentId());
                son.put("iconCls","icon-node");
                son.put("state", "closed");
                pdList.add(son);
                pd.put("menuID", sMenu.getId());
                pd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
                List<SystemButtonEntity> all = systemButtonService.findAll(pd);
                for(SystemButtonEntity sb:all){
                    PageData button = new PageData();
                    button.put("id", sb.getId()+2000);
                    button.put("menuName", sb.getButtonName());
                    button.put("_parentId", sMenu.getId()+1000);
                    button.put("menuURL", sb.getIconCls());
                    button.put("iconCls", sb.getIconCls());
                    button.put("parentId", "-1");
                    pdList.add(button);
                }
            }
        }
        PageData result = new PageData();
        result.put("total", count);
        result.put("rows", pdList);
        return result;
    }


    @RequestMapping("/menu-save/{id}/{parentId}")
    public Map<String,Object> menuSave(@PathVariable("id") String id,@PathVariable("parentId") String parentId){
        PageData pageData = this.getPageData();
        SystemMenuEntity systemMenuEntity = systemMenuService.findById(Integer.valueOf(id));
        if(null!=pageData.get("menu_name")&&!"".equals(pageData.get("menu_name").toString())){
            systemMenuEntity.setMenuName(pageData.getString("menu_name"));
        }
        if(null!=pageData.get("menu_icon")&&!"".equals(pageData.get("menu_icon").toString())){
            systemMenuEntity.setMenuIcon(pageData.getString("menu_icon"));
        }
        if(null!=pageData.get("menu_url")&&!"".equals(pageData.get("menu_url").toString())){
            systemMenuEntity.setMenuURL(pageData.getString("menu_url"));
        }
        if(null!=pageData.get("menu_parent")&&!"".equals(pageData.get("menu_parent").toString())){
            systemMenuEntity.setParentId(pageData.getString("menu_parent"));
        }
        //-1表示新增父页面
        if(-1==Integer.valueOf(id)){
            systemMenuEntity.setParentId("0");
            systemMenuEntity.setMenuURL("#");
        }
        //-2表示新增子页面
        if(-2==Integer.valueOf(id)){
            systemMenuEntity.setParentId(parentId);
        }
        SystemMenuEntity save = systemMenuService.save(systemMenuEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("result", "success");
        map.put("parentId", save.getParentId());
        return map;
    }

    @RequestMapping("/menu-delete")
    public String menuDelete(){
        PageData pageData = this.getPageData();
        String idStr = pageData.get("id").toString();
        try{
            Integer id = Integer.valueOf(idStr);
            SystemMenuEntity byId = systemMenuService.findById(id);
            byId.setDeleteFlag(InsUpdDelFlagConstant.DELETE_FLAG_Y);
            SystemMenuEntity save = systemMenuService.save(byId);
            return save.getParentId();
        }catch (Exception e){

        }
        return "";
    }

    @RequestMapping(value = "/menu-key-value")
    public Object constantsByType(){
        //先查redis，如果缓存有，直接返回，如果没有查询数据库，在将查询出来的结果存储到缓存中
        List<PageData> pageList = new ArrayList<>();
        PageData pageData = new PageData();
        pageData.put("parentId", "0");
        List<SystemMenuEntity> all = systemMenuService.findAll(pageData);
        for(SystemMenuEntity menu : all){
            PageData pd = new PageData();
            pd.put("key",menu.getMenuName());
            pd.put("value",menu.getId());
            pageList.add(pd);
        }
        return pageList;
    }

    @RequestMapping(value = "/menu-name/{id}")
    public Object menuNameById(@PathVariable("id") String id) {
        try{
            Integer idInteger = Integer.valueOf(id);
            //本应该从redis读取
            SystemMenuEntity byId = systemMenuService.findById(idInteger);
            return byId.getMenuName();
        }catch (NumberFormatException e){
            return "";
        }
    }
}
